﻿using System.Web;

namespace Api.Common
{
    /// <summary>
    /// Ajax參數助手類
    /// 用於從Request Form或QueryString中獲取參數，出錯時停止執行並返回Json字符串到客户端
    /// </summary>
    public class AjaxParameterHelper
    {
        public static string GetParams(string paramName)
        {
            string strValue = HttpContext.Current.Request.QueryString[paramName];
            if (!string.IsNullOrEmpty(strValue))
            {
                return strValue;
            }

            return HttpContext.Current.Request.Form[paramName];
        }

        #region 将字符串转化为uint?
        /// <summary>
        /// 获取一个参数，如果在查询中使用uint?
        /// </summary>
        /// <param name="paramName"></param>
        /// <param name="isAllowNull"></param>
        /// <returns></returns>
        public static uint? GetUIntOptional(string paramName, bool isAllowNull = true)
        {
            string strValue = GetParams(paramName);
            if (!string.IsNullOrEmpty(strValue))//不為空
            {
                uint value = 0;
                if (uint.TryParse(strValue, out value))//轉換成功
                {
                    return value;
                }
                else//轉換失敗
                {
                    AjaxMsgHelper.ResponseFail(paramName + "");
                    return value;
                }
            }
            else//為空
            {
                if (isAllowNull)
                {
                    return null;
                }
                else
                {
                    AjaxMsgHelper.ResponseFail(paramName + "不能為空");
                    return null;
                }
            }
        }
        #endregion

        #region 從Request Form或QueryString中獲取UInt類型的值
        /// <summary>
        /// 從Request Form或QueryString中獲取UInt類型的值
        /// </summary>
        /// <param name="paramName">參數名</param>
        /// <param name="isAllowNull">是否允許為空</param>
        /// <returns></returns>
        public static uint GetUInt(string paramName, bool isAllowNull = false)
        {
            string strValue = GetParams(paramName);
            if (!string.IsNullOrEmpty(strValue)) //不為空
            {
                uint value = 0;
                if (uint.TryParse(strValue, out value)) //轉換成功
                {
                    return value;
                }
                else //轉換失敗
                {
                    AjaxMsgHelper.ResponseFail(paramName + "格式錯誤");
                    return 0;
                }
            }
            else //為空
            {
                if (isAllowNull)
                {
                    return 0;
                }
                else
                {
                    AjaxMsgHelper.ResponseFail(paramName + "不能為空");
                    return 0;
                }
            }
        }
        #endregion

        #region 從Request Form或QueryString中獲取Int類型的值
        /// <summary>
        /// 從Request Form或QueryString中獲取Int類型的值
        /// </summary>
        /// <param name="paramName">參數名</param>
        /// <param name="isAllowNull">是否允許為空</param>
        /// <returns></returns>
        public static int GetInt(string paramName, bool isAllowNull = false)
        {
            string strValue = GetParams(paramName);
            if (!string.IsNullOrEmpty(strValue))//不為空
            {
                int value = 0;
                if (int.TryParse(strValue, out value))//轉換成功
                {
                    return value;
                }
                else//轉換失敗
                {
                    AjaxMsgHelper.ResponseFail(paramName + "格式錯誤");
                    return 0;
                }
            }
            else//為空
            {
                if (isAllowNull)
                {
                    return 0;
                }
                else
                {
                    AjaxMsgHelper.ResponseFail(paramName + "不能為空");
                    return 0;
                }
            }
        }
        #endregion

        #region 從Request Form或QueryString中獲取UShort類型的值
        /// <summary>
        /// 從Request Form或QueryString中獲取UShort類型的值
        /// </summary>
        /// <param name="paramName">參數名</param>
        /// <param name="isAllowNull">是否允許為空</param>
        /// <param name="defaultValue">默認值</param>
        /// <returns></returns>
        public static ushort GetUShort(string paramName, bool isAllowNull = false, ushort defaultValue = 0)
        {
            string strValue = GetParams(paramName);
            if (!string.IsNullOrEmpty(strValue))//不為空
            {
                ushort value = defaultValue;
                if (ushort.TryParse(strValue, out value))//轉換成功
                {
                    return value;
                }
                else//轉換失敗
                {
                    AjaxMsgHelper.ResponseFail(paramName + "格式錯誤");
                    return defaultValue;
                }
            }
            else//為空
            {
                if (isAllowNull)
                {
                    return defaultValue;
                }
                else
                {
                    AjaxMsgHelper.ResponseFail(paramName + "不能為空");
                    return defaultValue;
                }
            }
        }
        #endregion

        #region 從Request Form或QueryString中獲取Byte類型的值
        /// <summary>
        /// 從Request Form或QueryString中獲取Byte類型的值
        /// </summary>
        /// <param name="paramName">參數名</param>
        /// <param name="isAllowNull">是否允許為空</param>
        /// <param name="defaultValue">默認值</param>
        /// <param name="msg">提示消息</param>
        /// <returns></returns>
        public static byte GetByte(string paramName, bool isAllowNull = false, byte defaultValue = 0, string msg = null)
        {
            string strValue = GetParams(paramName);
            if (!string.IsNullOrEmpty(strValue))//不為空
            {
                byte value = defaultValue;
                if (byte.TryParse(strValue, out value))//轉換成功
                {
                    return value;
                }
                else//轉換失敗
                {
                    if (string.IsNullOrEmpty(msg))
                    {
                        AjaxMsgHelper.ResponseFail(paramName + "格式錯誤");
                    }
                    else
                    {
                        AjaxMsgHelper.ResponseFail(msg);
                    }
                    return defaultValue;
                }
            }
            else//為空
            {
                if (isAllowNull)
                {
                    return defaultValue;
                }
                else
                {
                    if (string.IsNullOrEmpty(msg))
                    {
                        AjaxMsgHelper.ResponseFail(paramName + "不能為空");
                    }
                    else
                    {
                        AjaxMsgHelper.ResponseFail(msg);
                    }
                    return defaultValue;
                }
            }
        }
        #endregion

        #region 從Request Form或QueryString中獲取String類型的值
        /// <summary>
        /// 從Request Form或QueryString中獲取String類型的值
        /// </summary>
        /// <param name="paramName">參數名</param>
        /// <param name="isAllowNull">是否允許為空</param>
        /// <param name="msg">提示消息</param>
        /// <returns></returns>
        public static string GetString(string paramName, bool isAllowNull = false, string msg = null)
        {
            string strValue = GetParams(paramName);
            if (strValue != null)//不為null
            {
                return strValue;
            }
            else//為null
            {
                if (isAllowNull)
                {
                    return null; //这里必须返回null，请不要修改 dan 2015/12/17 因为后台dal中的条件主要是根据hasvalue来决定要不要用sql语句来连接某个东西
                }

                if (string.IsNullOrEmpty(msg))
                {
                    AjaxMsgHelper.ResponseFail(paramName + "不能為空");
                }
                else
                {
                    AjaxMsgHelper.ResponseFail(msg);
                }
                return null;
            }
        }
        #endregion

        #region 從Request Form或QueryString中獲取bool類型的值
        /// <summary>
        /// 從Request Form或QueryString中獲取bool類型的值
        /// </summary>
        /// <param name="paramName">參數名</param>
        /// <param name="isAllowNull">是否允許為空</param>
        /// <returns></returns>
        public static bool GetBool(string paramName, bool isAllowNull = false)
        {
            string strValue = GetParams(paramName);
            if (!string.IsNullOrEmpty(strValue))//不為空
            {
                bool value = false;
                if (bool.TryParse(strValue, out value))//轉換成功
                {
                    return value;
                }
                else//轉換失敗
                {
                    AjaxMsgHelper.ResponseFail(paramName + "格式錯誤");
                    return false;
                }
            }
            else//為空
            {
                if (isAllowNull)
                {
                    return false;
                }
                else
                {
                    AjaxMsgHelper.ResponseFail(paramName + "不能為空");
                    return false;
                }
            }
        }
        #endregion

        #region 獲取網站Id(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// <summary>
        /// 獲取網站Id(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// </summary>
        /// <returns></returns>
        public static uint GetSiteId(bool isAllowNull = false)
        {
            return GetUInt("SiteId", isAllowNull);
        }
        #endregion

        #region 獲取频道Id(參數錯誤將直接輸出Json字符串並停止執行)
        /// <summary>
        /// 獲取频道Id(參數錯誤將直接輸出Json字符串並停止執行)
        /// </summary>
        /// <param name="isAllowNull">是否允許為空</param>
        /// <returns></returns>
        public static uint GetChannelId(bool isAllowNull = false)
        {
            return GetUInt("ChannelId", isAllowNull);
        }
        #endregion

        #region 獲取频道模块Id(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// <summary>
        /// 獲取频道模块Id(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// </summary>
        /// <param name="isAllowNull">是否允許為空</param>
        /// <returns></returns>
        public static uint GetCMId(bool isAllowNull = false)
        {
            return GetUInt("CMId", isAllowNull);
        }
        #endregion

        #region 獲取频道模块類型Id(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// <summary>
        /// 獲取频道模块Id(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// </summary>
        /// <param name="isAllowNull">是否允許為空</param>
        /// <returns></returns>
        public static uint GetCMTYPE(bool isAllowNull = false)
        {
            return GetUInt("CMTYPE", isAllowNull);
        }
        #endregion

        #region 獲取排序字段(為空或格式錯誤將直接輸出Json字符串並停止執行)
        /// <summary>
        /// 獲取排序字段(為空或格式錯誤將直接輸出Json字符串並停止執行)
        /// </summary>
        /// <returns></returns>
        public static string GetSortField(bool isAllowNull = false)
        {
            return GetString("SortField", isAllowNull);
        }
        #endregion

        #region 獲取是否是降序(為空或格式錯誤將直接輸出Json字符串並停止執行)
        /// <summary>
        /// 獲取是否是降序(為空或格式錯誤將直接輸出Json字符串並停止執行)
        /// </summary>
        /// <returns></returns>
        public static bool GetIsDesc(bool isAllowNull = false)
        {
            return GetBool("IsDesc", isAllowNull);
        }
        #endregion

        #region 獲取頁索引(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// <summary>
        /// 獲取頁索引(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// </summary>
        /// <returns></returns>
        public static ushort GetPageIndex(bool isAllowNull = true)
        {
            return GetUShort("PageIndex", isAllowNull, 1);
        }
        #endregion

        #region 獲取分頁大小(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// <summary>
        /// 獲取分頁大小(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// </summary>
        /// <returns></returns>
        public static ushort GetPageSize(bool isAllowNull = false)
        {
            return GetUShort("PageSize", isAllowNull);
        }
        #endregion

        #region
        /// <summary>
        /// 獲取BannerId(為空或參數錯誤將直接輸出Json字符串並停止執行)
        /// </summary>
        /// <param name="isAllowNull"></param>
        /// <returns></returns>
        public static uint GetBannerId(bool isAllowNull = false)
        {
            return GetUInt("BannerId", isAllowNull);
        }
        #endregion
    }
}
